package integraltesk

import (
	"context"
	"strconv"

	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetIntegralTaskByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetIntegralTaskByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIntegralTaskByIdLogic {
	return &GetIntegralTaskByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetIntegralTaskByIdLogic) GetIntegralTaskById(req *types.IDReq) (*types.IntegralTaskInfoResp, error) {
	logx.Infof("GetIntegralTaskById : %+v ", req)
	if req.Id == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	data, err := l.svcCtx.SettingRpc.BackendGetIntegralTaskById(l.ctx, &settingclient.UUIDReq{
		Id: strconv.FormatInt(int64(req.Id), 10),
	})
	if err != nil {
		return nil, err
	}

	option := utils.GetAuditOptionByStatus(data.Status)

	return &types.IntegralTaskInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.IntegralTaskInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         data.Id,
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			RuleName:      data.RuleName,
			RuleSlug:      data.RuleSlug,
			RuleType:      data.RuleType,
			PicId:         data.PicId,
			PicUrl:        data.PicUrl,
			Frequency:     data.Frequency,
			Points:        data.Points,
			EffectiveTime: data.EffectiveTime,
			Ip:            data.Ip,
			Description:   data.Description,
			Sort:          data.Sort,
			Status:        data.Status,
			DelStatus:     data.DelStatus,
		},
	}, nil
}
